package singleton;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-04-01
 * Time: 18:01
 */

//懒汉模式下实现单例
class Instance2 {
    volatile private static Instance2 instance2 = null;
    public static Instance2 getInstance2() {
        if (instance2==null) {
            synchronized (Instance2.class) {
                if (instance2==null) {
                    instance2 = new Instance2();
                }
            }
        }
        return instance2;
    }
}

public class SingletonLazy {
    public static void main(String[] args) {
        Instance2 I1 = Instance2.getInstance2();
        Instance2 I2 = Instance2.getInstance2();
        System.out.println(I1==I2);
    }
}
